package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.DangerMapper;
import com.smart.campus.dao.DetailsMapper;
import com.smart.campus.dao.SysUserMapper;
import com.smart.campus.dto.DangerDetails;
import com.smart.campus.dto.HiddenDanger;
import com.smart.campus.dto.SysUser;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.DangerService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@Service
public class DangerServiceImpl implements DangerService {

    @Autowired
    private DangerMapper dangerMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DetailsMapper detailsMapper;

    @Override
    public PagedResponse<HiddenDanger> findDangerPagination(HiddenDanger hiddenDanger, Integer pageNum, Integer pageSize) {
        Page<HiddenDanger> page = new Page<>(pageNum, pageSize);
        QueryWrapper<HiddenDanger> dangerQueryWrapper = new QueryWrapper<>();
        if(hiddenDanger.getDangerLevel()!=null && !hiddenDanger.getDangerLevel().equals("")){
            dangerQueryWrapper.eq("danger_level",hiddenDanger.getDangerLevel());
        }
        if(hiddenDanger.getDangerSource()!=null && !hiddenDanger.getDangerSource().equals("")){
            dangerQueryWrapper.eq("danger_source",hiddenDanger.getDangerSource());
        }
        if(hiddenDanger.getDangerType()!=null && !hiddenDanger.getDangerType().equals("")){
            dangerQueryWrapper.eq("danger_type",hiddenDanger.getDangerType());
        }
        if(hiddenDanger.getEscalationName()!=null && !hiddenDanger.getEscalationName().equals("")){
            dangerQueryWrapper.eq("escalation_name",hiddenDanger.getEscalationName());
        }
        if(hiddenDanger.getEscalationTimeStart()!=null && !hiddenDanger.getEscalationTimeStart().equals("")){
            dangerQueryWrapper.lt("escalation_time",hiddenDanger.getEscalationTimeStart());
        }
        if (hiddenDanger.getEscalationTimeEnd()!=null && !hiddenDanger.getEscalationTimeEnd().equals("")){
            dangerQueryWrapper.gt("escalation_time",hiddenDanger.getEscalationTimeEnd());
        }
        if (hiddenDanger.getEndTimeStart()!=null && !hiddenDanger.getEndTimeStart().equals("")){
            dangerQueryWrapper.lt("end_time",hiddenDanger.getEndTimeStart());
        }
        if (hiddenDanger.getEndTimeEnd()!=null && !hiddenDanger.getEndTimeEnd().equals("")){
            dangerQueryWrapper.gt("end_time",hiddenDanger.getEndTimeEnd());
        }

        //查询全部隐患
        if (hiddenDanger.getExamineState().equals(4)){//查询全部隐患
            Page<HiddenDanger> hiddenDangerPage = dangerMapper.selectPage(page, dangerQueryWrapper);
            return PaginationUtil.getPaginationResponse(hiddenDangerPage.getRecords(),pageNum,pageSize,hiddenDangerPage.getTotal());
        }else if(hiddenDanger.getExamineState().equals(5)){//查询个人上报隐患
            dangerQueryWrapper.eq("escalation_id",hiddenDanger.getEscalationId());
            Page<HiddenDanger> hiddenDangerPage = dangerMapper.selectPage(page, dangerQueryWrapper);
            return PaginationUtil.getPaginationResponse(hiddenDangerPage.getRecords(),pageNum,pageSize,hiddenDangerPage.getTotal());
        }else{//根据状态查询待处理，待验证，已完成隐患
            dangerQueryWrapper.eq("examine_state",hiddenDanger.getExamineState());
            Page<HiddenDanger> hiddenDangerPage = dangerMapper.selectPage(page, dangerQueryWrapper);
            return PaginationUtil.getPaginationResponse(hiddenDangerPage.getRecords(),pageNum,pageSize,hiddenDangerPage.getTotal());
        }
    }

    @Override
    public void addDanger(HiddenDanger hiddenDanger) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
        DangerDetails details = new DangerDetails();
        details.setDangerId(hiddenDanger.getDangerId());
        details.setSuperiorUnit("XX市教育局");
        hiddenDanger.setEscalationTime(format);
        hiddenDanger.setCreateTime(format);
        hiddenDanger.setUpdateTime(format);
        dangerMapper.insert(hiddenDanger);
        detailsMapper.insert(details);

        HashMap<String,Object> map = new HashMap<>();
    }

    @Override
    public HiddenDanger getDangerById(Long id) {
        HiddenDanger danger = dangerMapper.getDangerById(id);
        DangerDetails details = danger.getDetails();
        if (details.getUserId()!=null && !details.getUserId().equals("")){
            SysUser handleUser = sysUserMapper.selectById(details.getUserId());
            details.setHandleName(handleUser.getUserName());
        }
        if (details.getInspectId()!=null && !details.getInspectId().equals("")){
            SysUser inspectUser = sysUserMapper.selectById(details.getInspectId());
            details.setInspectName(inspectUser.getUserName());
        }
        danger.setDetails(details);
        return danger;
    }

    @Override
    public void assignPersonnelDanger(DangerDetails dangerDetails) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
        HiddenDanger hiddenDanger = dangerMapper.selectById(dangerDetails.getDangerId());
        Integer start = hiddenDanger.getExamineState();
        //根据隐患状态进行逻辑判断0-未审核1-待处理2-待验证3-已完成
        if (start.equals(0)){
            //指派人员对隐患进行处理
            dangerDetails.setAssignTime(format);
            dangerDetails.setCreateTime(format);
            dangerDetails.setUpdateTime(format);
            //在隐患详情表添加隐患详情信息
            detailsMapper.updateById(dangerDetails);
            hiddenDanger.setExamineState(1);
            hiddenDanger.setUpdateTime(format);
            //隐患表修改该条隐患状态
            dangerMapper.updateById(hiddenDanger);
        }else if(start.equals(1)){
            //专员上报处理结果
            dangerDetails.setUpdateTime(format);

            hiddenDanger.setExamineState(2);
            hiddenDanger.setHandleState(1);
            hiddenDanger.setUpdateTime(format);

            //更新隐患状态
            dangerMapper.updateById(hiddenDanger);
            //更新隐患详情信息
            detailsMapper.updateById(dangerDetails);
        }else if (start.equals(2)){
            //查看验证是否通过
            if (dangerDetails.getInspectState().equals(1)){
                //验证通过 修改状态已完成
                dangerDetails.setInspectTime(format);
                hiddenDanger.setExamineState(3);
                hiddenDanger.setUpdateTime(format);
                //更新隐患状态
                dangerMapper.updateById(hiddenDanger);
                detailsMapper.updateById(dangerDetails);
            }else {//未通过 修改状态重新处理
                dangerDetails.setUpdateTime(format);
                hiddenDanger.setExamineState(1);
                hiddenDanger.setHandleState(0);
                hiddenDanger.setUpdateTime(format);
                //更新隐患状态
                dangerMapper.updateById(hiddenDanger);
                //更新隐患详情信息
                detailsMapper.updateById(dangerDetails);
            }
        }
    }
}
